﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class TroopVehicle : BattleComponent<TroopVehicle> {
    public Ticker troopVehicleTicker = new Ticker(0, false);
    public TroopLeader leader;
    public TroopFormationPivot formationPivot;
    private bool DrawGizmos;
    public float TroopLeaderSpeedLerp;
    public float troopCloseEnoughDistance; //用来判定当部队中心点距离路径点的阈值判定距离

    public Vector3 targetPosition;
    public Quaternion targetRotation;

    private NavMeshPath path;
    private int NextPathCorner;
    public bool isReachTarget;
    public float TroopSpeed;
    public bool formationMove; //军团是否要以阵型移动



    private void OnEnable()
    {
        Assign(this);
    }

    private void OnDisable()
    {
        Unassign(this);
    }

    private void Start()
    {
        if (targetPosition == Vector3.zero) targetPosition = transform.position;
        if (targetRotation == Quaternion.identity) targetRotation = transform.rotation;
        RecaculatePath();
    }


    protected void RecaculatePath()
    {
        path = new NavMeshPath();
        NavMesh.CalculatePath(leader.centerPosition, targetPosition, NavMesh.AllAreas, path);
    }
    public static void UpdateTroopVehicle()
    {
        foreach(var vehicle in ComponentDataList)
        {
            if(vehicle.troopVehicleTicker.Tick())
            {
                vehicle.UpdateVehicle();
            }
        }
    }


    protected void UpdateVehicle()
    {
        isReachTarget = leader.transform.position.DistanceLessThan(targetPosition, troopCloseEnoughDistance);

        if (leader.troopState != TroopState.FormationMoving) return;

        if (path == null && transform.position != targetPosition)
        {
            RecaculatePath();
        }
        if (path != null && NextPathCorner < path.corners.Length)
        {
            TroopSpeed = Mathf.Lerp(TroopSpeed, leader.avgSpeed * formationPivot.formation.ArriveRatio, TroopLeaderSpeedLerp);
            transform.position = transform.position + (TroopSpeed * Time.deltaTime) * (path.corners[NextPathCorner] - transform.position).normalized;
            transform.rotation = Quaternion.LookRotation(path.corners[NextPathCorner] - transform.position);
            if (transform.position.DistanceLessThan(path.corners[NextPathCorner], troopCloseEnoughDistance))
            {
                NextPathCorner++;
            }
        }
        else
        {
            if (transform.position.DistanceLessThan(targetPosition, troopCloseEnoughDistance))
            {
                if (transform.position != targetPosition) leader.ReAssignFormation();
                transform.position = targetPosition;
                transform.rotation = targetRotation;
            }
            else
            {
                transform.position = transform.position + (leader.avgSpeed * formationPivot.formation.ArriveRatio * Time.deltaTime) * (targetPosition - transform.position).normalized;
                transform.rotation = Quaternion.LookRotation(targetPosition - transform.position);
            }
            path = null;
        }

    }

    private void OnDrawGizmos()
    {
        if (DrawGizmos)
        {
            Vector3 lp = leader.centerPosition;
            if (path != null)
            {
                foreach (var p in path.corners)
                {
                    Gizmos.color = Color.red;
                    Gizmos.DrawLine(lp, p);
                    Gizmos.DrawSphere(p, 2);
                    lp = p;
                }
            }
            Gizmos.color = Color.green;
            if (path != null && NextPathCorner < path.corners.Length)
            {
                Gizmos.DrawSphere(path.corners[NextPathCorner], 2f);
            }
        }
    }

}
